﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;

namespace SPNetworkTopology.ExternalWindows.ViewModel
{
    class ServiceLinkingViewModel : INotifyPropertyChanged
    {

        List<OptionViewModel<string>> serviceColumnCollection;
        List<string> selectedService = new List<string>();
        private bool isValid = true;
        RelayCommand cancelCommand;
        RelayCommand okCommand;

        public List<OptionViewModel<string>> SPServiceColumnCollection
        {
            get
            {
                if (serviceColumnCollection == null)
                {
                    this.SPServiceColumnCreation();
                }
                return serviceColumnCollection;
            }
        }

        #region OkCommand

        /// <summary>
        /// Returns the command which, when executed, cancels the order.      
        /// </summary>
        public ICommand OkCommand
        {
            get
            {
                if (okCommand == null)
                    okCommand = new RelayCommand(() => this.OkOrder(), () => this.isValid);

                return okCommand;
            }
        }

        void OkOrder()
        {
            ServiceLinking.SelectedService = this.selectedService;
            this.OnCancelRequestClose();
        }

        #endregion // CancelCommand

        #region CancelCommand

        /// <summary>
        /// Returns the command which, when executed, cancels the order.      
        /// </summary>
        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                    cancelCommand = new RelayCommand(() => this.CancelOrder());

                return cancelCommand;
            }
        }

        void CancelOrder()
        {
            this.OnCancelRequestClose();
        }       

        #endregion // CancelCommand

        /// <summary>
        /// Raise this event on button cancel click
        /// </summary>
        private void OnCancelRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
                handler(this, EventArgs.Empty);            
        }

        /// <summary>
        /// SharePoint service column creation
        /// </summary>
        private void SPServiceColumnCreation()
        {
            serviceColumnCollection = new List<OptionViewModel<string>>();

            List<string> columnCollection = ServiceLinking.AvailableServices.ToList();

            columnCollection.Insert(0, "Select All");

            foreach (string column in columnCollection)
            {
                OptionViewModel<string> columnCol = new OptionViewModel<string>(column, column);
                columnCol.IsSelected = true;
                columnCol.PropertyChanged += new PropertyChangedEventHandler(columnCol_PropertyChanged);
                this.selectedService.Add(column);
                serviceColumnCollection.Add(columnCol);
            }

            this.selectedService.Remove("Select All");
        }

        void columnCol_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            OptionViewModel<string> option = sender as OptionViewModel<string>;
            SelectionChangedAction(option, option.IsSelected);

            this.isValid = !this.selectedService.Count.Equals(0);
        }

        private void SelectionChangedAction(OptionViewModel<string> option, bool isSelected)
        {
            if (option.GetValue().Equals("Select All"))
            {
                serviceColumnCollection.ForEach(item =>
                {
                    if (!item.DisplayName.Equals("Server Name") && !item.DisplayName.Equals("Service Name"))
                    {
                        item.IsSelected = isSelected;
                    }
                });
                return;
            }
            else if (isSelected)
            {
                this.selectedService.Add(option.GetValue());
                return;
            }

            this.selectedService.Remove(option.GetValue());
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Raised when the wizard should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
